/***
 * 基于链表实现的栈结构
 */
public class LinkedListStack<E> implements Stack<E>{

    LinkedList<E> list;

    public LinkedListStack(){
        list = new LinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("LinkedListStack top :");
        sb.append(list.toString());
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedListStack linkedListStack = new LinkedListStack();
        for(int i=0;i<5;i++){
            linkedListStack.push(i);
            System.out.println(linkedListStack);
        }

        linkedListStack.pop();
        System.out.println(linkedListStack);
    }
}
